package com.linghu.databasecase;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * author : LingHu
 * e-mail : 1714873054@qq.com
 * date   : 2023/9/28 13:30
 * desc   :
 * version: 1.0
 */
public class Dao {
    private final DatabaseHelper helper;
    public Dao(Context context) {
        helper=new DatabaseHelper(context);
    }
    //增删改查
    public void insert(){
        SQLiteDatabase db = helper.getWritableDatabase();
        String sql=
                "insert into "+Constants.TABLE_NAME+"(id,name,age,salary) values(?,?,?,?)";
        db.execSQL(sql,new Object[]{1,"linghu",24,100});
        db.close();
    }
    public void update(){
        SQLiteDatabase db = helper.getWritableDatabase();
        String sql=
                "update "+Constants.TABLE_NAME+" set salary=12 where age=24";
        db.execSQL(sql);
        db.close();
    }
    public void delete(){
        SQLiteDatabase db = helper.getWritableDatabase();
        String sql=
                "delete from "+Constants.TABLE_NAME+" where id=1";
        db.execSQL(sql);
        db.close();
    }
    public void query(){
        SQLiteDatabase db = helper.getWritableDatabase();
        String sql=
                "select * from "+Constants.TABLE_NAME;
        Cursor cursor = db.rawQuery(sql, null);
        while (cursor.moveToNext()){
            int index = cursor.getColumnIndex("name");
            String name = cursor.getString(index);
            System.out.println("name="+name);
        }
        cursor.close();
        db.close();
    }
}
